package com.durian.base.net.monitor;


import com.durian.base.net.HttpResponse;
import com.durian.base.net.request.OkHttpTask;
import com.durian.base.net.Utils;

/**
 * 说明：LogMonitor
 * <p/>
 * 作者：fanly
 * <p/>
 * 类型：Class
 * <p/>
 * 时间：2019/2/16 11:08
 * <p/>
 * 版本：version 1.0
 */
public class LogMonitor implements HttpMonitorListener {
    @Override
    public void request(OkHttpTask task) {
        if (task != null) {
            Utils.vLog(String.format("请求ID = %s", task.getTaskId()));
            Utils.vLog(String.format("Url = %s", task.getUrl()));
            Utils.vLog(task.getRequestParams().toString());
        }
    }

    @Override
    public void response(HttpResponse response) {
        if (response != null) {
            StringBuilder sb = new StringBuilder();
            if (response.isSuccess()) {
                sb.append("请求成功").append("\n")
                        .append(String.format("Headers = %s", response.getHeaders().toString())).append("\n")
                        .append(String.format("HttpCode = %s", response.getHttpCode())).append("\n")
                        .append(String.format("Result = %s", response.getResult()));
            } else {
                sb.append("请求失败").append("\n")
                        .append(String.format("HttpCode = %s", response.getHttpCode())).append("\n")
                        .append(String.format("ErrorCode = %s", response.getErrorCode())).append("\n")
                        .append(String.format("Exception = %s", response.getException().toString()));
            }
            Utils.vLog(sb.toString());
        }
    }
}
